// XmlMsg.cpp: implementation of the CXmlMsg class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XmlMsg.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CXmlMsg::CXmlMsg()
{
	InitMsg();
	CoInitialize(NULL);
	pXMLDom.CreateInstance(__uuidof(DOMDocument40));
}

CXmlMsg::CXmlMsg(const CString &str)
{
	CoInitialize(NULL);
	pXMLDom.CreateInstance(__uuidof(DOMDocument40));

	if(IsAvail(str))
	{
		szBuf=str;
		DeCode();
	}
	else
		InitMsg();
}

CXmlMsg::CXmlMsg(CXmlMsg &msg)
{
	if(this!=&msg)
	{
		szBuf=msg.szBuf;
		DeCode();
	}
}

CXmlMsg::~CXmlMsg()
{
	pXMLDom.Release();
	CoUninitialize();
	szBuf.Empty();
}

CXmlMsg CXmlMsg::operator =(const CString &str)
{
	if(IsAvail(str))
	{
		szBuf=str;
		DeCode();
	}
	return *this;
}

CXmlMsg CXmlMsg::operator =(const CXmlMsg &msg)
{
	if(this!=&msg)
	{
		szBuf=msg.szBuf;
		DeCode();
	}
	return *this;
}

CXmlMsg::operator LPCTSTR()
{
	return (LPCTSTR)szBuf;
}

CXmlMsg::operator CString()
{
	return szBuf;
}

void CXmlMsg::InitMsg()
{
	szBuf.Empty();
	szBuf+=	"<message>";
	szBuf+=		"<form></form>";
	szBuf+=		"<to></to>";
	szBuf+=		"<content>";
	szBuf+=			"<num></num>";
	szBuf+=			"<str></str>";
	szBuf+=		"</content>";
	szBuf+=		"<from></from>";
	szBuf+=		"<time></time>";
	szBuf+=	"</message>";
}

CString CXmlMsg::EnCode()
{
	CString str;
	str.Format("%d",content.num);

	char tmpbuf[128];
	_strtime(tmpbuf);
	time.Format("%s",tmpbuf);

	if(IsAvail())
	{
		szBuf.Empty();
		szBuf+=	"<message>";
		szBuf+=		"<form>"+form+"</form>";
		szBuf+=		"<to>"+to+"</to>";
		szBuf+=		"<content>";
		szBuf+=			"<num>"+str+"</num>";
		szBuf+=			"<str><![CDATA["+content.str+"]]></str>";
		szBuf+=		"</content>";
		szBuf+=		"<from>"+from+"</from>";
		szBuf+=		"<time>"+time+"</time>";
		szBuf+=	"</message>";
	}
	else
		InitMsg();

	if(!IsAvail(szBuf))
		InitMsg();

	return szBuf;
}

BOOL CXmlMsg::DeCode()
{
	bool ret=false;
	IXMLDOMNodePtr pNode;

	if(!IsAvail(szBuf))
		ret=false;
	else
	{
		ret=true;
		pNode=pXMLDom->selectSingleNode("//form");
		if(pNode==NULL)
			ret=false;
		else
			form=(LPCTSTR)pNode->text;
		
		pNode=pXMLDom->selectSingleNode("//to");
		if(pNode==NULL)
			ret=false;
		else
			to=(LPCTSTR)pNode->text;

		pNode=pXMLDom->selectSingleNode("//num");
		if(pNode!=NULL)
			content.num=atoi((LPCTSTR)pNode->text);
		pNode=pXMLDom->selectSingleNode("//str");
		if(pNode!=NULL)
			content.str=(LPCTSTR)pNode->text;
		pNode=pXMLDom->selectSingleNode("//from");
		if(pNode!=NULL)
			from=(LPCTSTR)pNode->text;
		pNode=pXMLDom->selectSingleNode("//time");
		if(pNode!=NULL)
			time=(LPCTSTR)pNode->text;
	}

	if(pNode!=NULL)
		pNode.Release();
	return ret;
}

BOOL CXmlMsg::IsAvail(const CString &str)
{
	return (pXMLDom->loadXML((LPCSTR)str)==VARIANT_TRUE);
}

BOOL CXmlMsg::IsAvail()
{
	return (form.GetLength()!=0&&to.GetLength()!=0);
}
